package com.hn.project.common.job;

import cn.hutool.core.date.DateUtil;
import com.hn.common.enums.TaskTypeEnum;
import com.hn.common.utils.StringUtils;
import com.hn.common.utils.ding.DingNoticeUtil;
import com.hn.project.common.service.SysDictDataService;
import com.hn.project.rqinspect.domain.database.SysUser;
import com.hn.project.rqinspect.domain.database.TaskPushRecord;
import com.hn.project.rqinspect.domain.dto.TaskRecordDto;
import com.hn.project.rqinspect.mapper.TaskPushRecordMapper;
import com.hn.project.rqinspect.service.SysUserService;
import com.hn.project.rqinspect.service.TaskService;
import com.hn.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 巡检任务
 *
 * @author admin
 */
@Component("rqinspectTask")
public class RqinspectTask
{

    @Autowired
    private TaskService taskService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysDictDataService dictDataService;

    @Resource
    private TaskPushRecordMapper taskPushRecordMapper;

    public void sendTaskNotice(Integer pushRecordId,Integer userId){
        TaskRecordDto taskRecord = taskService.getTaskRecord(pushRecordId);
        String taskName = taskRecord.getTaskName();
        String branchName = taskRecord.getBranchName();
        Integer taskType = taskRecord.getTaskType();

        SysUser user = userService.getUserById(userId);
        AssertUtils.notNull(user,"推送任务通知失败 用户不存在,"+userId);

        String dingId = user.getDingId();
        AssertUtils.notNull(dingId,"推送任务通知失败 用户未绑定钉钉,"+userId);

        System.out.println("#### 任务推送 "+pushRecordId);
        String taskTypeName = dictDataService.getLabelName("task_type", taskType);

        String dingRecordId = DingNoticeUtil.sendTaskMsg(dingId, pushRecordId, taskName, taskTypeName,
                branchName);

        TaskPushRecord taskPushRecord = new TaskPushRecord();
        taskPushRecord.setId(pushRecordId);
        taskPushRecord.setUpdateTime(DateUtil.date());
        taskPushRecord.setDingRecordId(dingRecordId);
        taskPushRecordMapper.updateByPrimaryKeySelective(taskPushRecord);
    }
}
